package com.tky.conference.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.tky.conference.R;
import com.tky.conference.adapter.ConferenceAdapter;
import com.tky.conference.base.BaseActivity;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.dao.DeptInfo;
import com.tky.conference.dao.HysInfo;
import com.tky.conference.dao.TreeInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.implInterfaces.AppointInfoImpl;
import com.tky.conference.implInterfaces.DeptInfoImpl;
import com.tky.conference.implInterfaces.HysInfoImpl;
import com.tky.conference.implInterfaces.TreeInfoImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Created by yy on 2015/12/4.
 */
public class ConferenceActivity extends BaseActivity implements
        View.OnClickListener, AdapterView.OnItemClickListener ,HttpResultListener{

    private ListView appointListView;
    private Button addConference;
    private TextView isConference;
    private Toolbar toolbar;
    private String account;
    private List<AppointInfo> appointInfoList;
    private ShowDialog showDialog;
    private List<TreeInfo> TreeInfoList;
    private List<HysInfo> hysInfoList;
    private List<DeptInfo> deptInfoList;
    private Intent intent;
    boolean b1,b2,b3;
    private ConferenceAdapter adapter;



    @Override
    public void initView() {
        setContentView(R.layout.activity_conference);
        appointListView = (ListView) findViewById(R.id.lv_conference);
        addConference = (Button) findViewById(R.id.bt_add);
        isConference = (TextView) findViewById(R.id.tv_isConference);

    }

    @Override
    public void initDates() {
        showDialog = new ShowDialog(ConferenceActivity.this);
        SharedPreferences sp=this.getSharedPreferences("save_account",MODE_PRIVATE);
        account=sp.getString("account", null);
        appointInfoList = (List<AppointInfo>) getIntent().getSerializableExtra("APPINFOLIST");
         adapter = new ConferenceAdapter(this, appointInfoList,account);

        appointListView.setAdapter(adapter);
    }

    @Override
    public void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("预约会议");
        }
        setSupportActionBar(toolbar);
    }

    @Override
    public void initEvent() {
        appointListView.setOnItemClickListener(this);
        addConference.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                //点击添加开始请求数据
                //1.先请求数的数据
                b1=false;
                b2=false;
                b3=false;
                 intent = new Intent(BaseApplication.getContext(), AddActivity.class);


                DeptInfoImpl deptInfoImpl = new DeptInfoImpl(new DeptHttpResultListener());
                deptInfoImpl.getTreeConstruct(account);

                HysInfoImpl hysInfoImpl = new HysInfoImpl(new HysHttpResultListener());
                hysInfoImpl.hysInfo(account);

                TreeInfoImpl treeInfoImpl = new TreeInfoImpl(new TreeHttpResultListener());
                treeInfoImpl.getTreeInfo(account);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //当条目被点击时候获取详细信息
        Intent intent = new Intent(BaseApplication.getContext(), DetailActivity.class);
        AppointInfo appointInfo = appointInfoList.get(i);
        Bundle bundle = new Bundle();
        bundle.putSerializable("APPINFO", appointInfo);
        intent.putExtras(bundle);
        intent.putExtra("account", account);
        UIUtils.startActivity(intent);
    }

    @Override
    public void onStartRequest() {

    }

    @Override
    public void onResult(Object obj) {

    }

    @Override
    public void onFinish() {

    }

    class TreeHttpResultListener implements HttpResultListener {


        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {
                    ToastUtil.showToast(ConferenceActivity.this, ((FailRequest) obj).getMsg());
                }
            } else {

                TreeInfoList = (List<TreeInfo>) obj;
                intent.putExtra("TREELIST", (Serializable) TreeInfoList);
                b1=true;
                canGo();
            }


        }

        @Override
        public void onFinish() {

        }
    }

    class DeptHttpResultListener implements HttpResultListener{

        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {


                }
            } else {
                deptInfoList = (List<DeptInfo>) obj;
                intent.putExtra("DEPTLIST", (Serializable) deptInfoList);
                b2=true;
                canGo();
            }

        }

        @Override
        public void onFinish() {

        }
    }

    class HysHttpResultListener implements HttpResultListener {

        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {

                }
            } else {
                hysInfoList=(List<HysInfo>)obj;
                intent.putExtra("HYSLIST", (Serializable) hysInfoList);
                b3=true;
                canGo();

            }
        }

        @Override
        public void onFinish() {

        }
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void canGo(){
        if (b1&b2&b3){
            showDialog.dismissDialog();
            intent.putExtra("account", account);

            startActivityForResult(intent,1000);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            switch (requestCode){
                case 1000:
                    AppointInfo appointInfo= (AppointInfo) data.getSerializableExtra("appinfo");
                    appointInfoList.add(0, appointInfo);
                    adapter.notifyDataSetChanged();
                    break;
                case 1001:
                    AppointInfo appointInfo2= (AppointInfo) data.getSerializableExtra("appinfo");
                    int currentModifyItemPosition = adapter.getCurrentModifyItemPosition();
                    appointInfoList.remove(currentModifyItemPosition);
                    appointInfoList.add(0, appointInfo2);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }

}
